package net.tngou.crawler.drug;

import java.lang.reflect.GenericArrayType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.enums.StateEnum;
import net.tngou.crawler.pojo.Crawlerurl;
import net.tngou.crawler.pojo.POJO;
import net.tngou.crawler.util.StringUtil;




/**
 *  1药网
 * @author tngou@tngou.net
 *
 */
public class CN_COM_111 extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(CN_COM_111.class);
	
	
//	private String url="http://www.111.com.cn/product/{}.html";
	private String purl="http://www.111.com.cn/items/getComboInfo.action";
	

	@Override
	public JSONObject getJson(URL url) {
	
		String html = html(url.toString());
		if(html==null) return null;
		Document doc = Jsoup.parse(html);
	
		json.put("url", url);
		json.put("onlinepharmacy", onlinepharmacy);
		
		//价格 唯一
		Element product_amount = doc.getElementById("product_amount");
		if(product_amount!=null)
		{
			String itemid = product_amount.attr("itemid");
			json.put("un", itemid);
			json.put("price", getPrice(itemid));				
		}
		//标签
		Elements detailnavs = doc.getElementsByClass("detailnav");
		if(detailnavs.first()!=null)
		{			
			Elements as = detailnavs.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 1; i < as.size(); i++) {
				list.add(as.get(i).text());
			}				
			json.put("tag", list);
			
		}
		//图片
		Element thumblist = doc.getElementById("thumblist");
		if(thumblist!=null)
		{
			Elements as = thumblist.getElementsByTag("a");
			List<String> list = new ArrayList<>();
			as.forEach(e->{
				String rel = e.attr("rel");
				if(StringUtil.isNotEmpty(rel))
				{
					JSONObject js=(JSONObject) JSON.parse(rel);
					if(js!=null)
					{
						String largeimage = js.getString("largeimage");
						if(StringUtil.isNotEmpty(largeimage))
						{
							list.add(largeimage);
						}
					}
				}				
			});
			json.put("picture", list);
		}
		//处方类型
		Elements goods_intro = doc.getElementsByClass("goods_intro");
		if(goods_intro.first()!=null)
		{
			Element RX = goods_intro.first().getElementsMatchingOwnText("RX").first();
			Element OTC = goods_intro.first().getElementsMatchingOwnText("OTC").first();
			if(RX!=null)
			{
				json.put("ptype", Ptype.RX处方药.ordinal());
			}if(OTC!=null)
			{
				json.put("ptype", Ptype.OTC非处方甲类.ordinal());
				if(OTC.text().indexOf("乙")>0)
					json.put("ptype", Ptype.OTC非处方乙类.ordinal());
			}			
		}
		//说明书
		Element prodDetailCotentDiv = doc.body().getElementById("prodDetailCotentDiv");
		if(prodDetailCotentDiv!=null)
		{
			
			JSONObject instructions = instructions(prodDetailCotentDiv.text());
			Elements goods = doc.getElementsByClass("goods_intro");
			if(goods.first()!=null)instructions=instructions_tr_th_td(goods.first(),instructions);
			
			json.put("instructions",instructions );
			
		}

		return json;
	}
	
	



	public JSONObject instructions(String text) {		
		Document doc = Jsoup.parse(text);	
		JSONObject sjson = instructions_tr_th_td(doc.body());
		return instructions_tr_td_td(doc.body(),sjson);
	}
	
	public String html(String url) {
		log.info("请求页面:{}",url.toString());
		String html = HttpUtil.getString(url);
		if(html==null)return null;
		if(html.indexOf("很抱歉，您查看的页面找不到了！")>0)return null;
		return html;
		
	}
	
	public float getPrice(String itemid) {
		String rs = HttpUtil.getString(purl, new HashMap<String,String>() {{put("id", itemid);}});
		if(rs!=null)
		{	
			JSONArray rjs = (JSONArray) JSON.parse(rs);
			if(rjs!=null&&!rjs.isEmpty())
			{
				return rjs.getJSONObject(0).getFloatValue("originalPrice");
			}
		
		}
		return 0;
	}
	
	

	

	
}
